package org.example.exp2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.exp2.entity.Book;
import org.example.exp2.mapper.BookMapper;
import org.example.exp2.service.BookService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wml
 * @since 2024-06-06 03:32:50
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Override
    public IPage<Book> getBooks(int pageNum, int pageSize, String titleKeyword, String authorKeyword, String isbn, Boolean available) {
        // 构建查询条件并调用MyBatis Plus的query方法
        IPage<Book> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // 添加各种条件到QueryWrapper中...
        if (titleKeyword != null && !titleKeyword.isEmpty()) {
            queryWrapper.like("title", titleKeyword);
        }
        if (authorKeyword != null && !authorKeyword.isEmpty()) {
            queryWrapper.like("author", authorKeyword);
        }
        if (isbn != null && !isbn.isEmpty()) {
            queryWrapper.eq("isbn", isbn);
        }
        if (available != null) {
            queryWrapper.eq("available", available);
        }
        // 执行查询
        this.page(page, queryWrapper);
        return page;
    }
}
